﻿package {
	import fl.controls.progressBarClasses.IndeterminateBar;
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;

	public class Util extends Sprite {
				
		/**
		* The funtion to load the movie
		* 
		* @param	targetURL		This value specifies the url for the object to load
		* <p/>
		* @param	completeLoading  This value is the function to call when the loading is complete;		
		* <p/>
		*/
		public static function loadM(targetURL:String="", completeHandler:Function=null,processingHandler:Function=null,errorHandler:Function=null):void {
			try{
				var url:URLRequest = new URLRequest(targetURL);
				var loader:Loader = new Loader();
				//once load done, switch the scene by calling senceSwitch
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, (completeHandler!=null)?completeHandler:defaultCompeletHandler);
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, (processingHandler!=null)?processingHandler:defaultProcessingHandler);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, (errorHandler!=null)?errorHandler:defaultErrorHandler);
				loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, defaultErrorHandler);;
				loader.load(url);
			}catch (e:Error) {
				trace("<br>Error happened when perform loading:<br/> " +e.getStackTrace());
			}

		}
		
		public static function startSquenceLoad(objList:Array = null, completeHandler:Function = null,processingHandler:Function=null) {
			var list:Array = objList;
			if (list!=null && list.length != 0) {
				var obj:Object = list.pop();
				loadM(obj.url, function(e:Event){obj.func(e);startSquenceLoad(list,completeHandler,processingHandler);},processingHandler );	
			}else {
				completeHandler();
			}

		}
		public static function abs(num:Number):Number {
			return num >= 0?num: -num;
		}
		private static function defaultCompeletHandler(event:Event):void {
			trace("defaultCompeletHandler: " + event);
		}
		private static function defaultProcessingHandler(event:ProgressEvent):void {
			trace("defaultProcessingHandler: " + event);
		}
		private static function defaultErrorHandler(event:ErrorEvent):void {
            trace("errorHandler: " + event);
        }

	}
		
}
